package yunquan.wang.base.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.constants.AppConstants;
import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.dao.QueryHelper;
import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.domain.AuditableEntity;
import yunquan.wang.base.locale.LocaleMessage;
import yunquan.wang.base.service.BaseService;
import yunquan.wang.base.spring.security.SpringSecurityUtils;
import yunquan.wang.base.spring.security.UserDetail;
import yunquan.wang.modules.base.entity.UserBasic;
@Transactional(value = "jpaTransaction")
public abstract class BaseServiceImpl<T> implements BaseService<T> {

	protected  Logger logger = LoggerFactory.getLogger(this.getClass());
	protected  Logger dbLogger = LoggerFactory.getLogger(AppConstants.DBLOGER_NAME);
    
	protected ApplicationEventPublisher applicationEventPublisher;

	@Override
	public void setApplicationEventPublisher(
			ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zidei.core.service.BaseServiceImpl#getDao()
	 */
 
	public abstract BaseRepository<T, String> getDao();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zidei.core.service.BaseServiceImpl#save(T)
	 */
	@Override
	public T save(T entity) {
		String operation="";
 
		ServiceLog.log(this.getClass(), entity.toString(), operation, "");
		return getDao().save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zidei.core.service.BaseServiceImpl#findOne(java.lang.String)
	 */
	@Override
	public T get(String id) {
		ServiceLog.log(this.getClass(), id, LocaleMessage.getMessage("operation.query"), "");
		return getDao().findOne(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zidei.core.service.BaseServiceImpl#findAll()
	 */
	@Override
	public List<T> findAll() {
		ServiceLog.log(this.getClass(), "", LocaleMessage.getMessage("operation.query"), "");
		return getDao().findAll();
	}

	@Override
	public void delete(String id) {
		ServiceLog.log(this.getClass(), id,LocaleMessage.getMessage("operation.delete"), "");
		getDao().delete(id);
	}

	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zidei.core.service.BaseServiceImpl#findList(java.util.List)
	 */
	@Override
	public List<T> findList(Collection<PropertyFilter> filters) {
		Specification specification		=QueryHelper.listFilter(filters);
		return getDao().findAll(specification);
	}

 
	@Override
	public Page<T> findPage(Pageable page, Collection<PropertyFilter> filters) {
		Specification specification=QueryHelper.listFilter(filters);
		ServiceLog.log(this.getClass(),  page.toString(), LocaleMessage.getMessage("operation.query"), "");
		return getDao().findAll(specification, page);
	}

	public Page<T> findPage(Pageable page, Collection<PropertyFilter> filters,
			String... fetchProperties) {
 
		Specification specification		=QueryHelper.listFilter(filters);
		return getDao().findAll(specification, page);
	}
	
 

}
